#include <vector>
#include <iostream>
#include <limits.h>
#include <algorithm>

using namespace std;

int maxSize = 0;
bool inArea(vector<vector<int>> &data, int i, int j)
{
  return (i >= 0 && i < data.size()) && (j >= 0 && j < data[0].size());
}

void dfs(vector<vector<int>> &data, vector<vector<int>> &res, vector<int> &track, int i, int j, vector<vector<bool>> &visited)
{
  if (!inArea(data, i, j))
  {
    return;
  }

  if (visited[i][j] == true)
  {
    if (track.size() > maxSize)
    {
      maxSize = track.size();
    }
    return;
  }

  if (!track.empty())
  {
    int tmp = track[track.size() - 1];
    if (tmp < data[i][j])
    {
      if (track.size() > maxSize)
      {
        maxSize = track.size();
      }
      return;
    }
  }
  // 状态转移

  track.push_back(data[i][j]);
  visited[i][j] = true;

  dfs(data, res, track, i + 1, j, visited);
  dfs(data, res, track, i - 1, j, visited);

  dfs(data, res, track, i, j + 1, visited);
  dfs(data, res, track, i, j - 1, visited);

  track.pop_back();
  visited[i][j] = false;
}

int main()
{
  int rows, cols;
  cin >> rows >> cols;
  if (rows == 0 || cols == 0)
  {
    return 0;
  }
  vector<vector<int>> data(rows, vector<int>(cols));
  for (int i = 0; i < rows; i++)
  {
    for (int j = 0; j < cols; j++)
    {
      cin >> data[i][j];
    }
  }

  vector<vector<bool>> visited(rows, vector<bool>(cols));
  vector<vector<int>> res;
  vector<int> track;
  //int ans = INT_MIN;
  // 开始dfs找最长水沟

  // 这题每个点出发有可能，所以我们每个点都要开始dfs，最后取他们的最大值

  dfs(data, res, track, 0, 0, visited);

  cout << maxSize << endl;

  return 0;
}